package com.smile.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smile.commons.page.MyBatisPlusPageConverter;
import com.smile.commons.page.R;
import com.smile.model.dto.request.*;
import com.smile.model.response.response.GetOnSiteInspectionDetailResponseDTO;
import com.smile.model.response.response.GetOnSiteInspectionListResponseDTO;
import com.smile.model.request.*;
import com.smile.model.response.GetOnSiteInspectionDetailResponse;
import com.smile.model.response.GetOnSiteInspectionListResponse;
import com.smile.service.OnSiteInspectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutionException;

/**
 * 巡检计划controller
 *
 * @author 宁海博
 */
@RestController
@RequestMapping("/onSiteInspection")
public class OnSiteInspectionController {
    @Autowired
    private OnSiteInspectionService onSiteInspectionService;

    /**
     * 获取巡警计划列表
     *
     * @param getOnSiteInspectionListRequest
     * @return
     */
    @PostMapping("/getOnSiteInspectionList")
    public R<Page<GetOnSiteInspectionListResponse>> getOnSiteInspectionList(@RequestBody GetOnSiteInspectionListRequest getOnSiteInspectionListRequest) throws ExecutionException, InterruptedException {
        GetOnSiteInspectionListRequestDTO getOnSiteInspectionListRequestDTO = BeanUtil.copyProperties(getOnSiteInspectionListRequest, GetOnSiteInspectionListRequestDTO.class);
        Page<GetOnSiteInspectionListResponseDTO> page = onSiteInspectionService.getOnSiteInspectionList(getOnSiteInspectionListRequestDTO);
        return R.ok(MyBatisPlusPageConverter.convertPage(page, GetOnSiteInspectionListResponse.class));
    }

    /**
     * 获取巡警计划详情
     *
     * @param getOnSiteInspectionDetailRequest
     * @return
     */
    @PostMapping("/getOnSiteInspectionDetail")
    public R<GetOnSiteInspectionDetailResponse> getOnSiteInspectionDetail(@RequestBody GetOnSiteInspectionDetailRequest getOnSiteInspectionDetailRequest) {
        GetOnSiteInspectionDetailRequestDTO getOnSiteInspectionDetailRequestDTO = BeanUtil.copyProperties(getOnSiteInspectionDetailRequest, GetOnSiteInspectionDetailRequestDTO.class);
        GetOnSiteInspectionDetailResponseDTO getOnSiteInspectionDetailResponseDTO = onSiteInspectionService.getOnSiteInspectionDetail(getOnSiteInspectionDetailRequestDTO);
        return R.ok(BeanUtil.copyProperties(getOnSiteInspectionDetailResponseDTO, GetOnSiteInspectionDetailResponse.class));
    }

    /**
     * 添加巡警计划
     *
     * @param addOnSiteInspectionRequest
     * @return
     * @throws Exception
     */
//    @SaCheckPermission("计划管理")
    @PostMapping("/addOnSiteInspection")
    public R<Void> onSiteInspectionService(@Validated @RequestBody AddOnSiteInspectionRequest addOnSiteInspectionRequest) throws Exception {
        AddOnSiteInspectionRequestDTO addOnSiteInspectionRequestDTO = BeanUtil.copyProperties(addOnSiteInspectionRequest, AddOnSiteInspectionRequestDTO.class);
        onSiteInspectionService.onSiteInspectionService(addOnSiteInspectionRequestDTO);
        return R.ok();
    }

    /**
     * 编辑巡检计划
     *
     * @param editOnsiteInspectionRequest
     * @return
     */
    @PostMapping("/editOnsiteInspection")
//    @SaCheckPermission("计划管理")
    public R<Void> editOnsiteInspection(@RequestBody EditOnsiteInspectionRequest editOnsiteInspectionRequest) {
        EditOnsiteInspectionRequestDTO editOnsiteInspectionRequestDTO = BeanUtil.copyProperties(editOnsiteInspectionRequest, EditOnsiteInspectionRequestDTO.class);
        onSiteInspectionService.editOnsiteInspection(editOnsiteInspectionRequestDTO);
        return R.ok();
    }

    /**
     * 执行巡检计划
     *
     * @param executeOnSiteInspectionRequest
     * @return
     * @throws Exception
     */
    @PostMapping("/executeOnSiteInspection")
    @SaIgnore
    public R<Void> executeOnSiteInspection(@RequestBody ExecuteOnSiteInspectionRequest executeOnSiteInspectionRequest) throws Exception {
        ExecuteOnSiteInspectionRequestDTO executeOnSiteInspectionRequestDTO = BeanUtil.copyProperties(executeOnSiteInspectionRequest, ExecuteOnSiteInspectionRequestDTO.class);
        onSiteInspectionService.executeOnSiteInspection(executeOnSiteInspectionRequestDTO);
        return R.ok();
    }

    @PostMapping("/delete")
//    @SaCheckPermission("计划管理")
    public R<Void> delete(@RequestBody Long id) {
        onSiteInspectionService.delete(id);
        return R.ok();
    }

    @PostMapping("/deleteOnSiteInspectionByOrganizationId")
    public R<Void> deleteOnSiteInspectionByOrganizationId(@RequestBody Long id) {
        onSiteInspectionService.deleteOnSiteInspectionByOrganizationId(id);
        return R.ok();
    }

//    public R<Void> editSiteInspectionService(@RequestBody )

}
